'use strict';

const Controller = require('../core/baseController.js');
const crypto = require('crypto');

class UserController extends Controller {
  // 登录
  async login() {
    const { body } = this.ctx.request;
    const { username, password } = body;
    // 使用 md5 对密码进行加密
    const md5 = crypto.createHash('md5');
    const hashedPassword = md5.update(password).digest('hex');
    // 查询用户信息并比对密码
    const user = await this.app.mysql.get('users', { username });
    if (!user) {
      this.fail('用户不存在');
      return;
    }
    if (user.password !== hashedPassword) {
      this.fail('密码错误');
      return;
    }
    const token = this.app.jwt.sign({ username, id: user.id }, this.app.config.jwt.secret, {
      expiresIn: this.app.config.jwt.expiresIn,
    });
    this.success({
      token,
    });
  }
  // 获取用户信息
  async info() {
    // 从token中获取用户信息
    const token = this.ctx.request.header.authorization;
    const decode = this.app.jwt.verify(token, this.app.config.jwt.secret);
    this.success({
      name: decode.username,
      avatar: '',
    });
  }
  // 退出登录
  async logout() {
    this.success('退出成功');
  }
  // 获取用户列表
  async list() {
    const { ctx } = this;
    const { page, limit, username } = ctx.request.body;
    const offset = (page - 1) * limit;
    // 查询条件
    // const usernamesearch = `%${username}%`;
    // const list = await this.app.mysql.query(
    //   'select id, username, enabled, email, role from users where username like ? limit ? offset ?', [ usernamesearch, limit, offset ]
    // );
    // const totalCount = await this.app.mysql.query(
    //   'select count(1) from users where username like ?', [ usernamesearch ]
    // );
    // const total = totalCount[0]['count(1)'];
    const where = username ? { username } : {};
    const list = await this.app.mysql.select('users', {
      limit: parseInt(limit),
      columns: [ 'id', 'username', 'enabled', 'email', 'role' ],
      offset,
      where,
    });
    const total = await this.app.mysql.count('users', where);
    this.success({
      list,
      total,
    });
  }
  // 添加用户
  async add() {
    const { ctx } = this;
    const { username, password } = ctx.request.body;
    // 查询用户是否存在
    const user = await this.app.mysql.get('users', { username });
    if (user) {
      this.fail('用户已存在');
      return;
    }
    // 使用 md5 对密码进行加密
    const md5 = crypto.createHash('md5');
    const hashedPassword = md5.update(password).digest('hex');
    const result = await this.app.mysql.insert('users', {
      username,
      password: hashedPassword,
    });
    if (result.affectedRows === 1) {
      this.success('添加成功');
    } else {
      this.fail('添加失败');
    }
  }
  // 删除用户
  async delete() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const result = await this.app.mysql.update('users', {
      id,
      enabled: 0,
    });
    if (result.affectedRows === 1) {
      this.success('删除成功');
    } else {
      this.fail('删除失败');
    }
  }
}

module.exports = UserController;
